//
//  TopRightPopover.m
//  TopRightPopover
//
//  Created by Burn on 2019/1/11.
//  Copyright © 2019 nmgxysh. All rights reserved.
//

#import "TopRightPopover.h"

const CGFloat beginX = 15;

@implementation TopRightPopover

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}




- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGRect roundRect = CGRectMake(rect.origin.x, rect.origin.y + 8, rect.size.width, rect.size.height - 8);
    CGPoint moveP = CGPointMake(rect.size.width - beginX - 4, roundRect.origin.y);
    CGPoint secondP = CGPointMake(rect.size.width - beginX, 0);
    CGPoint endP = CGPointMake(rect.size.width - beginX + 4, roundRect.origin.y);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:roundRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(6, 6)];
    [path moveToPoint:moveP];
    [path addLineToPoint:secondP];
    [path addLineToPoint:endP];
    [path closePath];
    [[UIColor whiteColor] setFill];
    [path fill];
}

- (void)animation{
    CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    NSValue *value1 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    NSValue *value2 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.075, 1.075, 1)];
    NSValue *value3 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    scaleAnimation.values = @[value1,value2,value3];
    scaleAnimation.keyTimes = @[@0,@0.7,@1];
    
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
    baseAnimation.toValue = [NSNumber numberWithBool:false];
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = @[scaleAnimation,baseAnimation];
    groupAnimation.duration = 0.3;
    groupAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    groupAnimation.repeatCount = 1;
    
    self.layer.hidden = NO;
    [self.layer addAnimation:groupAnimation forKey:@"ani"];
}


@end
